﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace BOSS.Web
{
    /// <summary>
    /// 自定义的 ViewMasterPage 基类，提供一些常用函数
    /// </summary>
    public class BaseViewMasterPage : ViewMasterPage
    {
        /// <summary>
        /// 初始化许可
        /// </summary>
        /// <returns></returns>
        public string InitPermissions()
        {
            StringBuilder script = new StringBuilder(@"<script type=""text/javascript"">
(function() {
    if (typeof(Permissions) == ""undefined"") {
        window.Permissions = {};
        window.isAllow = function(permission) {
            var allow = window.Permissions[permission];
            if (typeof(allow) == ""undefined"") {
                return false;
            }
            return (allow == true);
        }
        window.isDeny = function(permission) {
            return !window.isAllow(permission);
        }
    }
");

            IDictionary<string, bool> permissions = ViewData[ViewDataConstants.PERMISSIONS] as IDictionary<string, bool>;
            if (permissions != null)
            {
                script.AppendLine("var p = window.Permissions;");
                foreach (var permission in permissions)
                {
                    script.Append("p[\"").Append(permission.Key).Append("\"]=");
                    if (permission.Value)
                    {
                        script.Append("true");
                    }
                    else
                    {
                        script.Append("false");
                    }
                    script.AppendLine(";");
                }
            }

            script.Append(@"}());
</script>");
            return script.ToString();
        }
    }

    /// <summary>
    /// 自定义的 ViewMasterPage 基类，提供一些常用函数
    /// </summary>
    /// <typeparam name="TModel">TModel is model</typeparam>
    public class BaseViewMasterPage<TModel> : ViewMasterPage<TModel> where TModel : class
    {
        /// <summary>
        /// 初始化许可
        /// </summary>
        /// <returns></returns>
        public string InitPermissions()
        {
            StringBuilder script = new StringBuilder(@"<script type=""text/javascript"">
(function() {
    if (typeof(Permissions) == ""undefined"") {
        window.Permissions = {};
        window.isAllow = function(permission) {
            var allow = window.Permissions[permission];
            if (typeof(allow) == ""undefined"") {
                return false;
            }
            return (allow == true);
        }
        window.isDeny = function(permission) {
            return !window.isAllow(permission);
        }
    }
");

            IDictionary<string, bool> permissions = ViewData[ViewDataConstants.PERMISSIONS] as IDictionary<string, bool>;
            if (permissions != null)
            {
                script.AppendLine("var p = window.Permissions;");
                foreach (var permission in permissions)
                {
                    script.Append("p[\"").Append(permission.Key).Append("\"]=");
                    if (permission.Value)
                    {
                        script.Append("true");
                    }
                    else
                    {
                        script.Append("false");
                    }
                    script.AppendLine(";");
                }
            }

            script.Append(@"}());
</script>");
            return script.ToString();
        }
    }

}
